﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Scheduler.Presentation_Layer
{
    /**Class: EmployeeProfile
     * @author Sara Tuohy
     * @version 1.0
     * Course : ITEC 4860 Spring 2012
     * Written: March, 2012
     * This class presents a form for the Employee Profile
     * Purpose: allow employee to create or modify his profile
     */
    public partial class EmployeeProfile : Form
    {
        /**
         * Constant for comment box feedback                            */
        private const int CHARACTER_LIMIT = 1000;
        /**
         * Id of user currently logged in                               */
        private string userId;
        /**
         * Employee's name                                              */
        private string employeeName;
        /**
         * Employee's phone                                             */
        private string phoneNum;
        /**
         * Employee's email address                                     */
        private string emailAddr;
        /**
         * Comment box data                                             */
        private string employeeComment;
        /**
         * booleans for formatting data saved feedback                  */
        private bool commentUpdated, phoneUpdated, emailUpdated;
        /**
         * DB table access adapter                                      */
        private Data_Access_Layer.Employee employeeData;
        /**
         * DB table access adapter                                      */
        private Data_Access_Layer.AcctManagement acctMgmtData;
        /**
         * bool for managing comments from the DB                       */
        private bool dataFound;

        /**Method: EmployeeProfile
         * Constructor
         * Purpose: Set defaults and call setUpPanel
         */
        public EmployeeProfile()
        {
            InitializeComponent();
            dataFound = false;
            commentUpdated = false;
            setUpPanel();
        }

        /**Method: setUpPanel
         * Preformat Panel with stored data
         * Purpose: read DB and populate form with save values
         */
        private void setUpPanel()
        {
            userId = Scheduler.Presentation_Layer.GlobalVariables.GlobalLoggedInUser;
            acctMgmtData = new Data_Access_Layer.AcctManagement(userId);
            bool acctFound = acctMgmtData.locateUser();
            if (acctFound)
            {
                employeeName = acctMgmtData.getName();
                phoneNum = acctMgmtData.getPhone();
                emailAddr = acctMgmtData.getEmail();

                epNameDisplayLabel.Text = employeeName;
                epPhoneMaskedTextBox.Text = phoneNum;
                epEmailTextBox.Text = emailAddr;

                //Setting the text "updated" the fields, so reset the booleans ...
                phoneUpdated = false;
                emailUpdated = false;

                dataFound = true;
            }
            else            //Should never, ever happen in production!
                MessageBox.Show("Unable to locate UserId " + userId);
            if (dataFound)
            {
                employeeData = new Data_Access_Layer.Employee(userId);
                if (employeeData.locateEmployee())
                    employeeComment = employeeData.getEmpComment();
                eProfCommentTextbox.Text = employeeComment;
                commentUpdated = false;
            }
        }

        /**Method: epPhoneMaskedTextBox_MaskInputRejected
         * Alerts user to Invalid input in Phone Number field
         * Purpose: provide specified error message on invalid
         * input.
         */
        private void epPhoneMaskedTextBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("“Incorrect phone number format: field must be numeric.", "Phone Number Edit",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /**Method: onFormClosing
         * Save any unsaved data
         * Purpose: to keep user from accidentally losing updates by exiting without saving
         * Note: Not yet implemented - needs to check for any changes and alert user
         */
        private void onFormClosing(object sender, EventArgs e)
        {
            if (commentUpdated)
                employeeData.setEmpComment(eProfCommentTextbox.Text);
        }

        /**Method: eProfCommentTextBox_TextChanged
         * Keeps track of number of characters in the comment box.
         * Purpose: Update the label that signifies the amount of
         * space remaining.
         */
        private void eProfCommentTextBox_TextChanged(object sender, EventArgs e)
        {
            string totalComment = eProfCommentTextbox.Text;
            int charCount = totalComment.Length;
            int roomLeft = CHARACTER_LIMIT - charCount;
            eProfCommentLimitLabel.Text = roomLeft.ToString();
            commentUpdated = true;
        }


        /**Method: eProfPhoneTextBox_TextChanged
         * Updates bool if the value has been changed
         */
        private void eProfPhoneTextBox_TextChanged(object sender, EventArgs e)
        {
            phoneUpdated = true;
        }

        /**Method: eProfeMailTextBox_TextChanged
         * Updates bool if the value has been changed
         */
        private void eProfeMailTextBox_TextChanged(object sender, EventArgs e)
        {
            emailUpdated = true;
        }

        /**Method: epSaveButton_Click
         * Save current values
         * Purpose: checks which of the updatable fields (phone, email, comment) has been 
         * changed and saves to the database.
         */
        private void epSaveButton_Click(object sender, EventArgs e)
        {
            string updInfo = "Saved to data base: ";
            string infoPlus;
            if (commentUpdated)
            {
                if (employeeData.locateEmployee())
                {
                    if (commentUpdated)
                    {
                        employeeData.setEmpComment(eProfCommentTextbox.Text);
                        updInfo += "Comments ";
                    }
                }
                else
                {
                    MessageBox.Show("Unable to locate Employee " + userId, "DB Save Comment Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if ((phoneUpdated) || (emailUpdated) || (commentUpdated))
            {
                infoPlus = savePhoneOrEmail();
                if (!infoPlus.Equals("Error"))      //if it IS "Error", msg box already displayed ...
                {
                    updInfo += infoPlus;
                    MessageBox.Show(updInfo, "DB Update",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                commentUpdated = false;
            }

        }

        /**Method: savePhoneOrEmail
         * Update data base with Current Information
         * Purpose: if the user has updated the contact information (phone or email or both)
         * save that info to the database, or alert to the error.
         * @return string moreInfo - will be "Error" if there was a problem or info on data
         * saved if there was not.
         */
        private string savePhoneOrEmail()
        {
            string moreInfo = " ";
            if (acctMgmtData.locateUser())
            {
                if (phoneUpdated)
                {
                    acctMgmtData.setPhone(epPhoneMaskedTextBox.Text);
                    moreInfo += "Phone ";
                    phoneUpdated = false;
                }
                if (emailUpdated)
                {
                    acctMgmtData.setEmail(epEmailTextBox.Text);
                    moreInfo += "Email ";
                    emailUpdated = false;
                }
            }
            else
            {
                MessageBox.Show("Unable to locate User: " + userId, "DB Save Phone/Email Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                moreInfo = "Error";
            }
            return moreInfo;
        }

        /**Method: epHomeButton_Click
         * Return to calling panel  
         */
        private void epHomeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
